/*
 * Gsoft开发框架
 * Copyright 2015-2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.itcs.rongyun.utils;

import java.util.HashMap;
import java.util.Map;

/**
 * 基本参数
 * @author LiuPeng
 * @date 2017年12月9日
 *  
 */
public class CommonParamFactory {
	
	
	/** 
	 * 实例
	 * @return 
	 */
	public static CommonParamFactory instance() {
		return new CommonParamFactory();
	}
	
	/** 
	 * 创建一个新的实例 CommonParamFactory.  
	 * 
	 */
	private CommonParamFactory() {
	}
	
	/** 
	 * 设置推送平台
	 * 目标操作系统，iOS、Android 最少传递一个。如果需要给两个系统推送消息时，则需要全部填写。（必传）
	 * ["ios","android"]
	 * @param platform
	 * @return 
	 */
	public CommonParamFactory platform(String[] platform ) {
		this.platform = platform;
		return this;
	}
	
	/** 
	 * 是否全部推送，false 表示按 tag 、tag_or 或 userid 条件推送，
	 * true 表示向所有用户推送，tag、tag_or 和 userid 条件无效。（必传）
	 * @param isToAll
	 * @return 
	 */
	public CommonParamFactory isToAll(boolean is_to_all) {
		this.is_to_all = is_to_all;
		return this;
	}
	
	/** 
	 * 用户 Id，每次发送时最多发送 1000 个用户，如果 tag 和 userid 两个条件同时存在时，
	 * 则以 userid 为准，如果 userid 有值时，则 platform 参数无效，is_to_all 为 true 时参数无效。（非必传）
	 * @param userid
	 * @return 
	 */
	public CommonParamFactory userid(String[] userid) {
		this.userid = userid;
		return this;
	}
	
	/** 
	 * 用户标签，每次发送时最多发送 20 个标签，标签之间为 AND 的关系，is_to_all 为 true 时参数无效。（非必传）
	 * @param tag
	 * @return 
	 */
	public CommonParamFactory tag(String[] tag) {
		this.tag = tag;
		return this;
	}
	
	/** 
	 * 用户标签，每次发送时最多发送 20 个标签，标签之间为 OR 的关系，
	 * is_to_all 为 true 时参数无效，tag_or 同 tag 参数可以同时存在。（非必传）
	 * @param tag_or
	 * @return 
	 */
	public CommonParamFactory tagOr(String[] tag_or) {
		this.tag_or = tag_or;
		return this;
	}
	
	/** 
	 * 通知栏显示的推送标题，仅针对 iOS 平台，支持 iOS 8.2 及以上版本，参数在 ios 节点下设置，详细可参考“设置 iOS 推送标题请求示例”。（非必传）
	 * @param alert
	 * @return 
	 */
	public CommonParamFactory title(String title) {
		this.title = title;
		return this;
	}
	
	/** 
	 * notification 下 alert，默认推送消息内容，如填写了 iOS 或 Android 下的 alert 时，
	 * 则推送内容以对应平台系统的 alert 为准。（必传）
	 * @param tag_or
	 * @return 
	 */
	public CommonParamFactory alert(String alert) {
		this.alert = alert;
		return this;
	}
	
	/** 
	 * iOS 或 Android 不同平台下的附加信息，如果开发者自己需要，可以自己在 App 端进行解析。（非必传）
	 * @param alert
	 * @return 
	 */
	public CommonParamFactory extras(Map<String, String> extras) {
		this.extras = extras;
		return this;
	}
	
	/** 
	 * 设置 iOS 平台下的推送及附加信息。
	 * {"title": "iOS 平台显示标题","alert": "iOS 平台显示内容","extras": {"id": "1","name": "2"}}
	 * @param iosNotification
	 * @return 
	 */
	public CommonParamFactory ios(Map<String, Object> iosNotification) {
		this.iosNotification = iosNotification;
		return this;
	}
	
	/** 
	 * 设置 Android 平台下的推送及附加信息。
	 * {"alert": "Android 平台显示内容","extras": {"id": "1","name": "2"}}
	 * @param androidNotification
	 * @return 
	 */
	public CommonParamFactory android(Map<String, Object> androidNotification) {
		this.androidNotification = androidNotification;
		return this;
	}
	
	private String[] platform = new String[] {"ios","android"};
	
	private boolean is_to_all = false;
	
	private String[] tag;
	
	private String[] tag_or;
	
	private String[] userid;
	
	private String title;
	
	private Map<String, String> extras;
	
	private String alert;
	
	private Map<String, Object> iosNotification;
	private Map<String, Object> androidNotification;
	
	/** 
	 * 构建参数
	 * @return 
	 */
	public Map<String, Object> build(){
		Map<String, Object> params = new HashMap<>();
		params.put("platform", platform);
		Map<String, Object> audience = new HashMap<>();
		if(is_to_all) {
			audience.put("is_to_all", is_to_all);
		}else if((tag!=null&&tag.length>0)||(tag_or!=null&&tag_or.length>0)) {
			if(tag!=null&&tag.length>0) {
				audience.put("tag", tag);
			}
			if(tag_or!=null&&tag_or.length>0) {
				audience.put("tag_or", tag_or);
			}
		}else if(userid!=null&&userid.length>0) {
			audience.put("userid", userid);
		}else {
			throw new RuntimeException("audience参数异常");
		}
		params.put("audience", audience);
		
		Map<String, Object> notification = new HashMap<>();
		if(alert!=null&&!"".equals(alert)) {
			notification.put("alert", alert);
		}else {
			throw new RuntimeException("alert参数异常");
		}
		
		
		if(iosNotification==null) {
			iosNotification = new HashMap<>();
			iosNotification.put("title", title);
			iosNotification.put("alert", alert);
			iosNotification.put("extras", extras);
		}
		notification.put("ios", iosNotification);
		
		if(androidNotification==null) {
			androidNotification = new HashMap<>();
			androidNotification.put("alert", alert);
			androidNotification.put("extras", extras);
		}
		notification.put("android", androidNotification);
		params.put("notification", notification);
		return params;
	}
}
